package com.ld.rightsSystem.mapper;

import com.ld.rightsSystem.dto.TRoleDTO;
import com.ld.rightsSystem.pojo.TPermission;
import com.ld.rightsSystem.pojo.TRole;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 角色信息表 Mapper 接口
 * </p>
 *
 * @author lhb
 * @since 2023-12-27
 */
@Repository
public interface TRoleRepository extends JpaRepository<TRole,Long> {

    @Transactional
    @Modifying
    @Query(value = "insert into auth.t_role (role_name,role_code,role_order,create_by,remark) " +
            "values (:#{#tRole.roleName},:#{#tRole.roleCode},:#{#tRole.roleOrder}," +
            ":#{#tRole.createBy},:#{#tRole.remark})",nativeQuery = true)
    int insert(@Param("tRole")TRole tRole);

    @Transactional
    @Modifying
    @Query(value = "update auth.t_role set role_name=:#{#tRole.roleName},role_code=:#{#tRole.roleCode}," +
            "role_order=:#{#tRole.roleOrder},update_by=:#{#tRole.updateBy},update_time=:#{#tRole.updateTime}," +
            "remark=:#{#tRole.remark} where role_id=:#{#tRole.roleId}",nativeQuery = true)
    int updateTRole(@Param("tRole")TRole tRole);

    @Query(value = "select * from auth.t_role where del_status=0 and role_id=:roleId",nativeQuery = true)
    TRole findTRoleByRoleId(@Param("roleId") Long roleId);

    @Query(value = "select role_id from auth.t_role where del_status=0 and role_name=:roleName",nativeQuery = true)
    Long findTRoleIdByRoleName(@Param("roleName") String roleName);

    @Query(value = "select role_id from auth.t_role where del_status=0 and role_code=:roleCode",nativeQuery = true)
    Long findTRoleIdByRoleCode(@Param("roleCode") String roleCode);

    @Query(value = "select * from auth.t_role where del_status=0",nativeQuery = true)
    List<TRole> findTRolesPageable(PageRequest pageRequest);

    @Transactional
    @Modifying
    @Query(value = "update auth.t_role set del_status=1,update_by=:updateBy,update_time=:updateTime where role_id=:roleId and del_status=0",nativeQuery = true)
    int deleteTRoleByRoleId(@Param("roleId")Long roleId, @Param("updateBy") String updateBy, @Param("updateTime") Date updateTime);
}
